package com.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.lang.Result;
import com.entity.Blog;
import com.service.BlogService;
import com.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChangAn
 * @since 2020-11-22
 */
@RestController
public class BlogController {
        @Autowired
        BlogService blogService;

    /**
     * 通过传入的page来查询博客信息
     * @param currentPage
     * @return
     */
       @GetMapping("/blogs")
       public Result blogs(Integer currentPage){
           if (currentPage==null||currentPage<1)
               currentPage=1;
           Page page = new Page(currentPage, 5);
           IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created"));
           return Result.success(pageData);
       }

    /**
     * 博客详情（通过博客id查询）
     * @param id
     * @return
     */
       @GetMapping("/blogs/{id}")
       public  Result detail(@PathVariable(name = "id")Long id){
           Blog blog = blogService.getById(id);
           Assert.notNull(blog, "当前博客已被删除");
           return Result.success(blog);
       }

    /**
     * 修改博客
     * @param blog
     * @return
     */
       @RequiresAuthentication//需要认证才能访问
       @PostMapping("/blog/edit")
       public Result edit(@Validated @RequestBody Blog blog){
           System.out.println(blog.toString());
           Blog tempb=null;
           if (blog.getId()!=null){
               tempb=blogService.getById(blog.getId());
               Assert.notNull(tempb, "没有这篇文章");
               Assert.isTrue(tempb.getUserId().longValue()== ShiroUtil.getProfile().getId(),"没有权限编辑");
           }else {
               System.out.println("blog的ID为null");//尽管blog别设置为自增
               tempb=new Blog();
               tempb.setUserId(ShiroUtil.getProfile().getId());
               tempb.setCreated(LocalDateTime.now());
               tempb.setStatus(0);
           }
           BeanUtils.copyProperties(blog, tempb,"userId", "created", "status");
           blogService.saveOrUpdate(tempb);
           return Result.success(null);
       }

    /**
     * 根据ID删除博客
     * @param id
     * @return
     */
       @RequiresAuthentication
       @DeleteMapping("/blogs/dele{id}")
       public Result dele(@PathVariable(name = "id")Long id){
           System.out.println(id);
           Blog blog = blogService.getById(id);
           System.out.println(blog);
           if (blog!=null) {
               Assert.isTrue(blog.getUserId().longValue() == ShiroUtil.getProfile().getId(), "没有权限删除");
           }else {
               return Result.fail("没有这条数据");
           }
           boolean b = blogService.removeById(id.longValue());
           if (b){
               return Result.success("删除成功",null);
           }
           return Result.fail("删除失败！");
       }
}
